
import RPi.GPIO as GPIO
import time

BUTTON_UP = 11, "Up"
BUTTON_LEFT = 9, "Left"
BUTTON_MIDDLE = 14, "Middle"
BUTTON_RIGHT = 15, "Right"
BUTTON_DOWN = 4, "Down"

buttons = BUTTON_UP, BUTTON_LEFT, BUTTON_MIDDLE, BUTTON_RIGHT, BUTTON_DOWN

funcdict = {
    #'callback' : callback_method
}

def gpio_init():
  GPIO.setwarnings(False)
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  for i in buttons:
    GPIO.setup(i[0], GPIO.IN)

gpio_init()

def wait_for_keypress(callback):
  funcdict[0] = callback
  while 1:
    for i in buttons:
      if GPIO.input(i[0]): 
        time.sleep(0.02)     # Debounce - wait and see if still pressed
        if GPIO.input(i[0]):
          funcdict[0](i[1], i[0])
	  time.sleep(0.3)    # Allow some time to let go of button
          if GPIO.input(i[0]):
            time.sleep(0.2)    # Let go of the damn button already!
    time.sleep(0.05)


